<script setup lang="ts">
import { ref } from 'vue';
import { useUserStore } from '../../stores/user';
import { updateCommonDataApi } from '../../api/user';
import {useRouter} from 'vue-router';

const userStore = useUserStore();
const router = useRouter();
const nickname = ref('');
const gender = ref('');
const birthday = ref('');
const bio = ref('这是一个大学生的个人简介');
const loading = ref(false);

function validateForm() {
  if (!nickname.value) {
    alert('请填写昵称！');
    return false;
  }
  if (!gender.value) {
    alert('请选择性别！');
    return false;
  }
  if (!birthday.value) {
    alert('请选择生日！');
    return false;
  }
  return true;
}

async function submitProfile() {
  if (!validateForm()) return;
  // 先存到 Pinia
  userStore.setUser({
    id: userStore.id,
    nickname: nickname.value,
    birthday: birthday.value,
    gender: gender.value,
    bio: bio.value,
    email: userStore.email || '',
    LuojiaAccount: userStore.LuojiaAccount || ''
  });
  loading.value = true;
  try {
    const result = await updateCommonDataApi(
      Number(userStore.id),
      nickname.value,
      gender.value,
      bio.value,
      birthday.value,
      userStore.LuojiaAccount || '',
      userStore.email || '')
    
  console.log('result', result);
  if (result.code === 200) {
    alert('信息提交成功，欢迎加入课托邦！');
    userStore.updateUserInfo({
      nickname: nickname.value,
      birthday: birthday.value,
      gender: gender.value,
      bio: bio.value
    });
    // 首页或其他页面
    router.push('/forumhome');
  } else {
    alert((result.data && result.data.message) || '提交失败！');
  }

  } catch (error) {
    alert(error.response?.data?.message || '信息提交失败，请稍后再试！');
    loading.value = false;
    return;
  } finally {
    loading.value = false;
  }
}
</script>

<template>
  <div class="first-log-container">
    <div class="first-log-card-row">
      <div class="first-log-card-left">
        <form @submit.prevent="submitProfile">
          <label class = "form-label">昵称
            <input v-model="nickname" type="text" placeholder="请输入昵称" required />
          </label>
          <label class = "form-label">性别
            <div class="gender-row">
              <label><input type="radio" value="M" v-model="gender" /> 男</label>
              <label><input type="radio" value="F" v-model="gender" /> 女</label>
            </div>
          </label>
          <label class = "form-label">生日
            <input v-model="birthday" type="date" required />
          </label>
          <label class = "form-label">个人简介
            <textarea v-model="bio" rows="3" placeholder="这是一个大学生的个人简介"></textarea>
          </label>
          <button class="submit-btn" type="submit" :disabled="loading">提交</button>
        </form>
      </div>
      <div class="first-log-card-right">
        <h2>欢迎加入课托邦！</h2>
        <p class="intro">课托邦是专为大学生打造的学习交流与行程管理平台，结合个性化AI辅助，助你高效学习、轻松规划大学生活！</p>
        <div class="welcome-img-placeholder">图片预留区</div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.first-log-container {
  min-height: 100vh;
  min-width: 100vw;
  width: 100vw;
  height: 100vh;
  position: fixed;
  top: 0;
  left: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--gradient-bg);
  z-index: 1000;
}
.first-log-card-row {
  display: flex;
  flex-direction: row;
  background: var(--bg-primary);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  border: 1px solid var(--border-light);
  min-width: 700px;
  min-height: 420px;
  overflow: hidden;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
.first-log-card-row:hover {
  border-color: var(--light-purple);
  box-shadow: var(--shadow-purple);
}
.first-log-card-left {
  flex: 1;
  padding: 40px 32px 32px 32px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
.first-log-card-right {
  flex: 1;
  background: var(--gradient-bg);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 32px 32px 32px;
}
.first-log-card-right h2 {
  color: var(--primary-blue);
  margin-top: 0;
  margin-bottom: 12px;
  text-align: center;
}
.intro {
  color: var(--text-muted);
  font-size: 15px;
  margin-bottom: 24px;
  text-align: center;
}
form {
  display: flex;
  flex-direction: column;
  gap: 16px;
}
label {
  font-size: 14px;
  color: var(--primary-blue);
  margin-bottom: 4px;
}
input[type="text"],
input[type="date"],
textarea {
  padding: 10px 12px;
  border: 1px solid var(--border-light);
  border-radius: var(--radius-md);
  font-size: 15px;
  outline: none;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  display: block;
  width: 100%;
  box-sizing: border-box;
  margin-top: 4px;
}
input:focus, textarea:focus {
  border: 1.5px solid var(--primary-purple);
  box-shadow: var(--shadow-purple);
}
.gender-row {
  display: flex;
  gap: 16px;
  margin-top: 4px;
}
.gender-row label {
  color: var(--text-primary);
}
.submit-btn {
  margin-top: 8px;
  background: var(--gradient-purple);
  color: #ffffff;
  border: none;
  border-radius: var(--radius-md);
  padding: 10px 0;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
.submit-btn:hover:not(:disabled) {
  background: linear-gradient(135deg, #9333ea 0%, #6d28d9 100%);
  transform: translateY(-2px);
  box-shadow: var(--shadow-purple);
}
.submit-btn:disabled {
  opacity: 0.7;
  cursor: not-allowed;
  background: var(--light-purple);
}

.form-label {
  color: var(--primary-blue);
  font-weight: bold;
  text-align: left;
}
.welcome-img-placeholder {
  width: 180px;
  height: 120px;
  background: var(--bg-secondary);
  border-radius: var(--radius-md);
  border: 1px solid var(--border-light);
  margin-top: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-light);
  font-size: 15px;
}
</style>
